home *** CD-ROM | disk | FTP | other *** search
- /*
- File: SIHshTbl.cpp
-
- Contains: Implementation of SIHashTable class
-
- Owned by: Nick Pilch
-
- Copyright: © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
-
-
- */
-
- #ifndef _SIHSHTBL_
- #include "SIHshTbl.h"
- #endif
-
- #ifndef _HshTbl_
- #include "HshTbl.h"
- #endif
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #ifndef _ODTYPES_
- #include "ODTypes.h"
- #endif
-
- #ifndef _UTILERRS_
- #include "UtilErrs.h"
- #endif
-
- #ifndef __AEOBJECTS__
- #include <AEObjects.h>
- #endif
-
- #ifndef _ODDEBUG_
- #include "ODDebug.h"
- #endif
-
- #pragma segment SIHashTable
-
- //==============================================================================
- // Implementation comments
- //==============================================================================
-
- /*
- The error codes returned from Hash Manager functions are incompletely
- documented.
- Therefore, I make some pessimistic assumptions. If creating a new hash table or
- any other routine that may need to allocate memory fails, I assume that it's
- out of memory and throw kODErrOutOfMemory. Otherwise, I don't check error codes
- explicitly.
- */
-
- #define kMaxValueSize 10
- //==============================================================================
- // SIHashTable
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // SIHashTable::SIHashTable
- //------------------------------------------------------------------------------
-
- SIHashTable::SIHashTable()
- {
- fSIHashTable = kODNULL;
- fValueSize = 0 ;
- }
-
- //------------------------------------------------------------------------------
- // SIHashTable::Initialize
- //------------------------------------------------------------------------------
-
- void SIHashTable::Initialize(ODULong numEntries, ODUShort keySize,
- ODUShort valueSize, ODBoolean inSysHeap)
- {
- if ( valueSize > kMaxValueSize )
- THROW( kODErrHashValueSizeTooBig );
- OSErr error = NewHashTable(numEntries, keySize, valueSize,
- (MemProcBlock*)NULL, inSysHeap, &fSIHashTable);
- if (error || !fSIHashTable)
- THROW(kODErrOutOfMemory);
- fValueSize = valueSize ;
- }
-
- //------------------------------------------------------------------------------
- // SIHashTable::~SIHashTable
- //------------------------------------------------------------------------------
-
- SIHashTable::~SIHashTable()
- {
- if (fSIHashTable)
- DisposeHashTable(&fSIHashTable, NULL);
- }
-
- //------------------------------------------------------------------------------
- // SIHashTable::ReplaceEntry
- //------------------------------------------------------------------------------
-
- void SIHashTable::ReplaceEntry(ODKeyPtr key, ODEntryPtr value)
- {
- OSErr error = ::ReplaceEntry(fSIHashTable, NULL, (KeyPtr)key, (HEntryPtr)value);
- if (error)
- THROW(kODErrOutOfMemory);
- }
-
- //------------------------------------------------------------------------------
- // SIHashTable::RemoveEntry
- //------------------------------------------------------------------------------
-
- void SIHashTable::RemoveEntry(ODKeyPtr key)
- {
- // As far as I could tell, RemoveKeyEntry never returns an error.
-
- RemoveKeyEntry(fSIHashTable, NULL, (KeyPtr)key);
- }
-
- //------------------------------------------------------------------------------
- // SIHashTable::GetValue
- //------------------------------------------------------------------------------
-
- ODBoolean SIHashTable::GetValue(ODKeyPtr key, ODEntryPtr value)
- {
- if (GetKeyValue(fSIHashTable, NULL, (KeyPtr)key, (HEntryPtr)value) != noErr)
- return kODFalse;
- else
- return kODTrue;
- }
-
- //------------------------------------------------------------------------------
- // SIHashTable::Exists
- //------------------------------------------------------------------------------
-
- ODBoolean SIHashTable::Exists(ODKeyPtr key)
- {
- char someScratchValueSpace[kMaxValueSize] ;
- WASSERT(fValueSize <= kMaxValueSize);
- // if ( fValueSize > kMaxValueSize )
- // THROW( -1000 ) ; // <eeh> need a real # here
-
- OSErr err = GetKeyValue( fSIHashTable, kODNULL, (KeyPtr)key,
- (HEntryPtr)someScratchValueSpace ) ;
- return err == noErr ;
- }
-
- //------------------------------------------------------------------------------
- // SIHashTable::GetSIHashTable
- //------------------------------------------------------------------------------
-
- HashTable SIHashTable::GetSIHashTable()
- {
- return fSIHashTable;
- }
-
- //==============================================================================
- // SIHashTableIterator
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // SIHashTableIterator::SIHashTableIterator
- //------------------------------------------------------------------------------
-
- SIHashTableIterator::SIHashTableIterator(SIHashTable* table)
- {
- fTable = table;
- fIndex = 0;
- fDone = kODFalse;
- }
-
- //------------------------------------------------------------------------------
- // SIHashTableIterator::~SIHashTableIterator
- //------------------------------------------------------------------------------
-
- SIHashTableIterator::~SIHashTableIterator()
- {
- }
-
- //------------------------------------------------------------------------------
- // SIHashTableIterator::First
- //------------------------------------------------------------------------------
-
- void SIHashTableIterator::First(ODKeyPtr key, ODEntryPtr value)
- {
- if (!this->GetNext(key, value))
- fDone = kODTrue;
- }
-
- //------------------------------------------------------------------------------
- // SIHashTableIterator::Next
- //------------------------------------------------------------------------------
-
- void SIHashTableIterator::Next(ODKeyPtr key, ODEntryPtr value)
- {
- if (!this->GetNext(key, value))
- fDone = kODTrue;
- }
-
- //------------------------------------------------------------------------------
- // SIHashTableIterator::IsNotComplete
- //------------------------------------------------------------------------------
-
- ODBoolean SIHashTableIterator::IsNotComplete()
- {
- return !fDone;
- }
-
- //------------------------------------------------------------------------------
- // SIHashTableIterator::GetNext
- //
- // Returns true if another entry can be found, false otherwise.
- //------------------------------------------------------------------------------
-
- ODBoolean SIHashTableIterator::GetNext(ODKeyPtr key, ODEntryPtr value)
- {
- OSErr result = noErr;
-
- do
- {
- result = GetIndexedEntry(fTable->GetSIHashTable(),
- kODNULL, fIndex, (KeyPtr)key, (HEntryPtr)value);
- ++fIndex;
- if (result == noErr)
- return kODTrue;
- }
- while (result != kAEErrEndOfTable);
-
- return kODFalse;
- }
-
-